﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SoundPlayer : MonoBehaviour {

	public float fxVolume = 1f;

	public List<AudioClip> lstClipRythmic = new List<AudioClip>();
	public List<AudioClip> lstClipMelodyTurc = new List<AudioClip>();
	public List<AudioClip> lstClipMelodyDisco = new List<AudioClip>();

	public List<AudioClip> lstClipSoundEffect = new List<AudioClip>();

	private List<AudioSource> lstRythmic = new List<AudioSource>();
	private List<AudioSource> lstMelodyTurc = new List<AudioSource>();
	private List<AudioSource> lstMelodyDisco = new List<AudioSource>();

	private int rythmicValue = 0;
	private int melodyTurcValue = 0;
	private int melodyDiscoValue = 0;


	private void Awake(){
		foreach (AudioClip clip in lstClipRythmic)
		{
			lstRythmic.Add(AddAudio(clip,true,true,0));
		}
		foreach (AudioClip clip in lstClipMelodyTurc)
		{
			lstMelodyTurc.Add(AddAudio(clip,true,true,0));
		}
		foreach (AudioClip clip in lstClipMelodyDisco)
		{
			lstMelodyDisco.Add(AddAudio(clip,true,true,0));
		}
	}

	public void Update()
	{
		rythmicValue = Mathf.Max (GameSystem.scoreFerdinor, GameSystem.scoreIgor);
		melodyDiscoValue = (GameSystem.scoreFerdinor - GameSystem.scoreIgor >= 0 ? GameSystem.scoreFerdinor - GameSystem.scoreIgor : 0);
		melodyTurcValue = (GameSystem.scoreIgor - GameSystem.scoreFerdinor >= 0 ? GameSystem.scoreIgor - GameSystem.scoreFerdinor : 0);

		if (rythmicValue == 1) {
			lstRythmic[0].volume -= 0.01f;
			lstRythmic[1].volume += 0.1f;
			lstRythmic[2].volume -= 0.1f;
			lstRythmic[3].volume -= 0.1f;
			lstRythmic[4].volume -= 0.1f;
		} else if (rythmicValue == 2) {
			lstRythmic[0].volume -= 0.1f;
			lstRythmic[1].volume -= 0.1f;
			lstRythmic[2].volume += 0.1f;
			lstRythmic[3].volume -= 0.1f;
			lstRythmic[4].volume -= 0.1f;
		} else if (rythmicValue == 3) {
			lstRythmic[0].volume -= 0.1f;
			lstRythmic[1].volume -= 0.1f;
			lstRythmic[2].volume -= 0.1f;
			lstRythmic[3].volume += 0.1f;
			lstRythmic[4].volume -= 0.1f;
		} else if (rythmicValue == 4) {
			lstRythmic[0].volume -= 0.1f;
			lstRythmic[1].volume -= 0.1f;
			lstRythmic[2].volume -= 0.1f;
			lstRythmic[3].volume -= 0.1f;
			lstRythmic[4].volume += 0.1f;
		} else {
			lstRythmic[0].volume = 1;
			lstRythmic[1].volume -= 0.1f;
			lstRythmic[2].volume -= 0.1f;
			lstRythmic[3].volume -= 0.1f;
			lstRythmic[4].volume -= 0.1f;
		}

		if (melodyTurcValue == 1) {
			lstMelodyTurc[0].volume += 0.1f;
			lstMelodyTurc[1].volume -= 0.1f;
			lstMelodyTurc[2].volume -= 0.1f;
		} else if (melodyTurcValue == 2) {
			lstMelodyTurc[0].volume -= 0.1f;
			lstMelodyTurc[1].volume += 0.1f;
			lstMelodyTurc[2].volume -= 0.1f;
		} else if (melodyTurcValue >= 3) {
			lstMelodyTurc[0].volume -= 0.1f;
			lstMelodyTurc[1].volume -= 0.1f;
			lstMelodyTurc[2].volume += 0.1f;
		} else {
			for(int i =0;i<lstMelodyTurc.Count;i++)
			{
				lstMelodyTurc[i].volume -= 0.1f;
			}
		}

		if (melodyDiscoValue == 1) {
			lstMelodyDisco[0].volume += 0.1f;
			lstMelodyDisco[1].volume -= 0.1f;
			lstMelodyDisco[2].volume -= 0.1f;
		} else if (melodyDiscoValue == 2) {
			lstMelodyDisco[0].volume -= 0.1f;
			lstMelodyDisco[1].volume += 0.1f;
			lstMelodyDisco[2].volume -= 0.1f;
		} else if (melodyDiscoValue >= 3) {
			lstMelodyDisco[0].volume -= 0.1f;
			lstMelodyDisco[1].volume -= 0.1f;
			lstMelodyDisco[2].volume += 0.1f;
		} else {
			for(int i =0;i<lstMelodyDisco.Count;i++)
			{
				lstMelodyDisco[i].volume -= 0.1f;
			}
		}
	}
	
	AudioSource AddAudio(AudioClip clip,bool loop,bool playAwake,float vol)
	{
		AudioSource newAudio = gameObject.AddComponent<AudioSource>();
		newAudio.clip = clip;
		newAudio.loop = loop;
		newAudio.playOnAwake = playAwake;
		newAudio.volume = vol;
		newAudio.Play ();
		return newAudio;
	}
	
	public void playSound(int index) {
		AudioSource.PlayClipAtPoint (lstClipSoundEffect[index], transform.position, fxVolume);
	}

	public void LaunchCoroutSound(int index,float delay)
	{
		StartCoroutine(CoroutPlaySound(index, delay));
	}

	public IEnumerator CoroutPlaySound(int index,float delay)
	{
		yield return new WaitForSeconds (delay);
		AudioSource.PlayClipAtPoint (lstClipSoundEffect[index], transform.position, fxVolume);
	}
}
